package edu.uoc.tds.pac4.client.ss3;

import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;

import edu.uoc.tds.pac4.beans.Centre;
import edu.uoc.tds.pac4.beans.TipusEvent;
import edu.uoc.tds.pac4.beans.Universitat;
import edu.uoc.tds.pac4.common.PantallaError;
import edu.uoc.tds.pac4.common.TDSLanguageUtils;

public class JPanelFiltreIngresosTotals extends JPanel {

	private static final long serialVersionUID = 1L;
	protected static final int defaultHeight = 20;
	private JLabel jLabelUniversitat = null;  
	private JLabel jLabelCentre = null;  
	private JLabel jLabelTipusEvent = null;
	private JLabel jLabelIngresMinim = null;
	private JLabel jLabelIngresMaxim = null;
	private JComboBox jComboBoxUniversitat = null;
	private JComboBox jComboBoxCentre = null;  
	private JComboBox jComboBoxTipusEvent = null;
	private JTextField jTextFieldIngresMinim = null;
	private JTextField jTextFieldIngresMaxim = null;
	private JButton jButtonFiltre = null; 
	private InterfaceFiltreEstadistiques pantalla = null;
	
	/**
	 * This is the default constructor
	 */
	public JPanelFiltreIngresosTotals(InterfaceFiltreEstadistiques pantalla) {
		this.pantalla = pantalla;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gbcButtonFiltre = new GridBagConstraints();
		gbcButtonFiltre.gridx = 6;
		gbcButtonFiltre.gridy = 1;
		gbcButtonFiltre.insets = new Insets(5, 10, 5, 10);

		// Universitat
		GridBagConstraints gbcLabelUniversitat = new GridBagConstraints();
		gbcLabelUniversitat.gridx = 0;
		gbcLabelUniversitat.gridy = 0;
		gbcLabelUniversitat.anchor = GridBagConstraints.LINE_END;
		jLabelUniversitat = new JLabel();
		jLabelUniversitat.setText(TDSLanguageUtils
				.getMessage("Client.PantallaPorcentatgeAssistencia.UniversitatNom"));
		GridBagConstraints gbcComboBoxUniversitat = new GridBagConstraints();
		gbcComboBoxUniversitat.gridx = 1;
		gbcComboBoxUniversitat.gridy = 0;
		gbcComboBoxUniversitat.insets = new Insets(5, 10, 5, 10);
		// Centre
		GridBagConstraints gbcLabelCentre = new GridBagConstraints();
		gbcLabelCentre.gridx = 0;
		gbcLabelCentre.gridy = 1;
		gbcLabelCentre.anchor = GridBagConstraints.LINE_END;
		jLabelCentre = new JLabel();
		jLabelCentre.setText(TDSLanguageUtils
				.getMessage("Client.PantallaPorcentatgeAssistencia.CentreNom"));
		GridBagConstraints gbcComboBoxCentre = new GridBagConstraints();
		gbcComboBoxCentre.gridx = 1;
		gbcComboBoxCentre.gridy = 1;
		gbcComboBoxCentre.insets = new Insets(5, 10, 5, 10);
		// TipusEvent
		GridBagConstraints gbcLblTipusEvent = new GridBagConstraints();
		gbcLblTipusEvent.gridx = 0;
		gbcLblTipusEvent.gridy = 2;
		gbcLblTipusEvent.anchor = GridBagConstraints.LINE_END;
		jLabelTipusEvent = new JLabel();
		jLabelTipusEvent.setText(TDSLanguageUtils
				.getMessage("Client.PantallaPorcentatgeAssistencia.TipusEvent"));
		GridBagConstraints gbcComboBoxTipusEvent = new GridBagConstraints();
		gbcComboBoxTipusEvent.weightx = 0.7;
		gbcComboBoxTipusEvent.gridx = 1;
		gbcComboBoxTipusEvent.gridy = 2;
		gbcComboBoxTipusEvent.insets = new Insets(5, 10, 5, 10);

		//Ingressos
		GridBagConstraints gbcTextIngresMinim = new GridBagConstraints();
		gbcTextIngresMinim.gridx = 5;
		gbcTextIngresMinim.gridy = 0;
		gbcTextIngresMinim.insets = new Insets(5, 10, 5, 10);
		GridBagConstraints gbcLabelIngresMinim = new GridBagConstraints();
		gbcLabelIngresMinim.gridx = 4;
		gbcLabelIngresMinim.gridy = 0;
		gbcLabelIngresMinim.anchor = GridBagConstraints.LINE_END;
		jLabelIngresMinim = new JLabel();
		jLabelIngresMinim.setText(TDSLanguageUtils
				.getMessage("Client.PantallaPorcentatgeAssistencia.IngresMinim"));

		GridBagConstraints gbcTextIngresMaxim = new GridBagConstraints();
		gbcTextIngresMaxim.gridx = 5;
		gbcTextIngresMaxim.gridy = 1;
		gbcTextIngresMaxim.insets = new Insets(5, 10, 5, 10);
		GridBagConstraints gbcLabelIngresMaxim = new GridBagConstraints();
		gbcLabelIngresMaxim.gridx = 4;
		gbcLabelIngresMaxim.gridy = 1;
		gbcLabelIngresMaxim.anchor = GridBagConstraints.LINE_END;
		jLabelIngresMaxim = new JLabel();
		jLabelIngresMaxim.setText(TDSLanguageUtils
				.getMessage("Client.PantallaPorcentatgeAssistencia.IngresMaxim"));
		// JPanel
		this.setLayout(new GridBagLayout());
		this.setName("jPanelFiltre");
		this.add(jLabelUniversitat, gbcLabelUniversitat);
		this.add(getJComboBoxUniversitat(), gbcComboBoxUniversitat);
		this.add(jLabelCentre, gbcLabelCentre);
		this.add(getJComboBoxCentre(), gbcComboBoxCentre);
		this.add(getJButtonFiltre(), gbcButtonFiltre);
		this.add(jLabelTipusEvent, gbcLblTipusEvent);
		this.add(getJComboBoxTipusEvent(), gbcComboBoxTipusEvent);
		this.add(jLabelIngresMinim,gbcLabelIngresMinim);
		this.add(getJTextFieldIngresMinim(),gbcTextIngresMinim);
		this.add(jLabelIngresMaxim,gbcLabelIngresMaxim);
		this.add(getJTextFieldIngresMaxim(),gbcTextIngresMaxim);
	}

	/**
	 * Retorna el centre seleccionat
	 * 
	 * @return
	 */
	public Centre getCentre() {
		Centre currentCentre = null;
		if (jComboBoxCentre.getSelectedItem() instanceof Centre)
			currentCentre = (Centre) jComboBoxCentre.getSelectedItem();
		else
			currentCentre = null;

		return currentCentre;
	}

	/**
	 * Retorna la universitat seleccionada
	 * 
	 * @return
	 */
	public Universitat getUniversitat() {
		Universitat currentUniversitat = null;
		if (jComboBoxUniversitat.getSelectedItem() instanceof Universitat)
			currentUniversitat = (Universitat) jComboBoxUniversitat.getSelectedItem();
		else
			currentUniversitat = null;

		return currentUniversitat;
	}

	/**
	 * Retorna El tipus d'event Seleccionat
	 * 
	 * @return
	 */
	public TipusEvent getTipusEvent() {
		TipusEvent currentTipusEvent = null;
		if (jComboBoxTipusEvent.getSelectedItem() instanceof TipusEvent)
			currentTipusEvent = (TipusEvent) jComboBoxTipusEvent.getSelectedItem();
		else
			currentTipusEvent = null;

		return currentTipusEvent;
	}
	

	
	/**
	 * Retorna Ingres Minim
	 * 
	 * @return
	 */
	public Double getIngresMinim(){
		Double IngresMinim = 0.0;
		if (!jTextFieldIngresMinim.getText().isEmpty()){
			IngresMinim = Double.valueOf(jTextFieldIngresMinim.getText());
		}
		return IngresMinim;
		
	}
	/**
	 * Retorna Ingres Maxim
	 * 
	 * @return
	 */
	public Double getIngresMaxim(){
		Double IngresMaxim = 0.0;
		if (!jTextFieldIngresMaxim.getText().isEmpty()){
			IngresMaxim = Double.valueOf(jTextFieldIngresMaxim.getText());
		}
		return IngresMaxim;
		
	}

	
	/**
	 * Inicialitza el boto Filtrar
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonFiltre() {
		if (jButtonFiltre == null) {
			jButtonFiltre = new JButton();
			jButtonFiltre.setText(TDSLanguageUtils
				.getMessage("Client.PantallaPorcentatgeAssistencia.Filtre"));
			jButtonFiltre.setSize(200, 20);
			jButtonFiltre.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {

					pantalla.updateTaula();
				}
			});
		}
		return jButtonFiltre;
	}

	/**
	 * Inicialitza jComboBoxUniversitat
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBoxUniversitat() {
		if (jComboBoxUniversitat == null) {
			jComboBoxUniversitat = new JComboBox();
			jComboBoxUniversitat.setMinimumSize(new Dimension(200, 20));
			jComboBoxUniversitat.setPreferredSize(new Dimension(200, 20));
			jComboBoxUniversitat.addItem(TDSLanguageUtils
					.getMessage("Client.PantallaPorcentatgeAssistencia.Tots"));
			Vector<Universitat> universitats = pantalla.carregarUniversitats();
			Helpers.setItemsCombo(universitats, jComboBoxUniversitat);
			jComboBoxUniversitat.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					carregarCentres();
				}
			});
		}
		return jComboBoxUniversitat;
	}

	/**
	 * Carrega els centres alcombo
	 */
	private void carregarCentres() {
		jComboBoxCentre.removeAllItems();
		jComboBoxCentre.addItem(TDSLanguageUtils
				.getMessage("Client.PantallaPorcentatgeAssistencia.Tots"));
		Vector<Centre> Centres = pantalla.carregarCentres(getUniversitat());
		Helpers.setItemsCombo(Centres,jComboBoxCentre);
	}
	
	/**
	 * Inicialitza jComboBoxCentre
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBoxCentre() {
		if (jComboBoxCentre == null) {
			jComboBoxCentre = new JComboBox();
			jComboBoxCentre.setMinimumSize(new Dimension(200, 20));
			jComboBoxCentre.setPreferredSize(new Dimension(200, 20));
			jComboBoxCentre.addItem(TDSLanguageUtils
					.getMessage("Client.PantallaPorcentatgeAssistencia.Tots"));
			Vector<Centre> Centres = pantalla.carregarCentres(getUniversitat());
			Helpers.setItemsCombo(Centres, jComboBoxCentre);
		}
		
		return jComboBoxCentre;
	}
	
	/**
	 * Inicialitza jComboBoxTipusEvent
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getJComboBoxTipusEvent() {
		if (jComboBoxTipusEvent == null) {
			jComboBoxTipusEvent = new JComboBox();
			jComboBoxTipusEvent.setMinimumSize(new Dimension(200, 20));
			jComboBoxTipusEvent.setPreferredSize(new Dimension(200, 20));
			jComboBoxTipusEvent.addItem(TDSLanguageUtils
					.getMessage("Client.PantallaPorcentatgeAssistencia.Tots"));
			Vector<TipusEvent> TipusEvents = pantalla.carregarTipusEvents();
			Helpers.setItemsCombo(TipusEvents, jComboBoxTipusEvent);
			
		}
		return jComboBoxTipusEvent;
	}


	 /** Inicialitza jTextFieldIngresMinim
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextFieldIngresMinim() {
		if (jTextFieldIngresMinim == null) {
			jTextFieldIngresMinim = new JTextField();
			jTextFieldIngresMinim
					.setMinimumSize(new Dimension(80, defaultHeight));
			jTextFieldIngresMinim.setPreferredSize(new Dimension(80,
					defaultHeight));
			jTextFieldIngresMinim.addFocusListener(new java.awt.event.FocusAdapter() {
				public void focusLost(java.awt.event.FocusEvent e) {
					if (!jTextFieldIngresMinim.getText().isEmpty()){
						validaDouble(jTextFieldIngresMinim.getText());
					}
				}
			});
		}
		return jTextFieldIngresMinim;
	}
	
	/**
	 * Inicialitza jTextFieldIngresMaxim
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextFieldIngresMaxim() {
		if (jTextFieldIngresMaxim == null) {
			jTextFieldIngresMaxim = new JTextField();
			jTextFieldIngresMaxim
					.setMinimumSize(new Dimension(80, defaultHeight));
			jTextFieldIngresMaxim.setPreferredSize(new Dimension(80,
					defaultHeight));
			jTextFieldIngresMaxim.addFocusListener(new java.awt.event.FocusAdapter() {
				public void focusLost(java.awt.event.FocusEvent e) {
					if (!jTextFieldIngresMaxim.getText().isEmpty()){
						validaDouble(jTextFieldIngresMaxim.getText());
					}
				}
			});
		}
		return jTextFieldIngresMaxim;
	}

	private void validaDouble(String valor){
		try {
		   Double.parseDouble(valor);
		}
		catch(NumberFormatException e) {
			new PantallaError("Error_Format_Double", PantallaError.Tipus.ERROR);
		}

	}
	
}

